﻿using Ivory;
using Lidgren.Network;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Paintball.Libaries;
using Paintball.Network;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Paintball
{
    public class Player : Sprite, IAbstractable
    {
        //Fields
        public override int LayerIndex
        {
            get
            {
                return Globals.PlayerLayerIndex;
            }
            set
            {
                RenderIndex = Globals.PlayerLayerIndex; 
            }
        }
        
        public int PlayerID { get; set; }
        public string Username { get; set; }
        public Vector2 WorldPosition;
                
        //ctor
        public Player(int playerID, Vector2 startPosition)
            : base(new Rectangle((int)startPosition.X, (int)startPosition.Y, 64, 76))
        {
            this.PlayerID = playerID;
        }
        public Player()
            : base(new Rectangle(0, 0, 64, 76))
        { }

        //Methods
        public override void Draw(SpriteBatch batch, GameTime time)
        {
            batch.Begin();

            batch.Draw(Resources.PlayerTexture, BoundingBox, null, Color.Blue, Rotation, Origin, SpriteEffects.None, 0);

            batch.End();
        }
        public override void Update(InputControl input, GameTime time)
        {

        }
        public override bool Load()
        {
            return true;
        }

        public void Write(NetOutgoingMessage outMsg)
        {
            outMsg.Write(Rotation);
            outMsg.Write(Username);
            outMsg.Write(PlayerID);
            outMsg.Write(WorldPosition.X);
            outMsg.Write(WorldPosition.Y);
        }
        public void Read(NetIncomingMessage incMsg)
        {
            this.Rotation = incMsg.ReadFloat();
            this.Username = incMsg.ReadString();
            this.PlayerID = incMsg.ReadInt32();
            this.WorldPosition = new Vector2();
            this.WorldPosition.X = incMsg.ReadFloat();
            this.WorldPosition.Y = incMsg.ReadFloat();
        }
    }
}
